# -*- coding: utf-8 -*-

from django.db import models
from core.models import CoreModel, table_prefix
import reversion


"""协议类型"""


class ProtocolType(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="protocol_type_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    name = models.CharField(max_length=128, verbose_name="协议名称")
    type = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="协议类别"
    )
    name_cn = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="中文名称"
    )
    name_en = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="英文名称"
    )

    def __str__(self) -> str:
        return self.name

    class Meta:
        unique_together = (("product", "name"),)
        db_table = table_prefix + "rms_protocol_type"
        verbose_name = "协议类型"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)


"""协议字段"""


@reversion.register()
class ProtocolField(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="protocol_field_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    protocol = models.ForeignKey(
        to="ProtocolType",
        related_name="protocol_field_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="协议名称",
    )
    name_cn = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="中文名称"
    )
    name_en = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="英文名称"
    )
    name = models.CharField(max_length=128, verbose_name="字段名称")
    sdx = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="字段别名"
    )
    type = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="字段类型"
    )
    length = models.IntegerField(null=True, blank=True, verbose_name="字段长度")
    desc = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="字段描述"
    )
    tshark = models.CharField(
        null=True, blank=True, max_length=256, verbose_name="过滤条件"
    )
    isjx = models.CharField(max_length=256, default="Y", verbose_name="是否解析")
    istl = models.CharField(max_length=256, default="N", verbose_name="是否通联")
    issx = models.CharField(max_length=256, default="N", verbose_name="是否筛选")

    def __str__(self) -> str:
        return self.name

    class Meta:
        unique_together = (("product", "protocol", "name"),)
        db_table = table_prefix + "rms_protocol_field"
        verbose_name = "协议字段"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)
